﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevFx.Model;
using DevFx.Web.Code;
using DevFx.FrameWork;

namespace DevFx.Web.Pages.Systems
{
    public partial class MenuEdit : BasePage
    {
        #region 变量
        
        #endregion

        #region 属性
       
        /// <summary>
        /// ID
        /// </summary>
        public Guid MID
        {
            get {

                if (ViewState["MID"] == null )
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                    {
                        ViewState["MID"] = Request.QueryString["id"].ToString();
                    }
                    else
                    {
                        ViewState["MID"] = Guid.Empty;
                    }
                }
                Guid g=new Guid( ViewState["MID"].ToString());
                return g;
            }
            set { ViewState["MID"] = value; }
        }
        /// <summary>
        /// 当前操作的对象
        /// </summary>    
        private SysMenu MenuEntityInfo
        {
            get
            {
                if (ViewState["MenuEntityInfo"] == null)
                {
                    if (!( MID==Guid.Empty))
                    {
                        using (Entities db = new Entities())
                        {
                            ViewState["MenuEntityInfo"] = db.SysMenus.Where(m=>m.MenuID==MID).FirstOrDefault();
                        }
                    }
                    else
                    {
                        SysMenu menu = new SysMenu();
                        if (MID == Guid.Empty)
                        {
                            menu.MenuID = Guid.NewGuid();
                        }
                        menu.CreateBy = UserID;
                        menu.CreateDate = DateTime.Now;
                        menu.ModifyBy =UserID;
                        menu.ModifyDate = DateTime.Now;
                        ViewState["MenuEntityInfo"] = menu;
                    }

                }
                return ViewState["MenuEntityInfo"] as SysMenu;
            }
            set
            {
                ViewState["MenuEntityInfo"] = value;
            }
        }
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            
            using (Entities db = new Entities())
            {
               // GetData();
                if (IsAdd)
                {
                    GetData(MenuEntityInfo);
                    db.AddToSysMenus(MenuEntityInfo);
                }
                else if (IsEdit)
                {
                    var menu = db.SysMenus.FirstOrDefault(m => m.MenuID == MID);
                    GetData(menu);
                }
                db.SaveChanges();
            }
            MessageBox.ShowAndCloseRefrashJQueryDialog(this.Page, "保存成功！","");
          
        }
        #endregion

        #region 方法
        private void BindData()
        {
            this.lblMenuID.Text = MenuEntityInfo.MenuID.ToString();
            if (IsEdit)
            {
                this.tbxMenuName.Text = MenuEntityInfo.Name.ToString();
                this.tbxMenuEnName.Text = MenuEntityInfo.Code == null ? "" : MenuEntityInfo.Code.ToString();
                this.tbxIcon.Text = MenuEntityInfo.Icon == null ? "" : MenuEntityInfo.Icon.ToString();
                this.tbxLevel.Text = MenuEntityInfo.Level.ToString();
                this.tbxOrderNo.Text = MenuEntityInfo.OrderNo.ToString();
                this.tbxParentID.Text = MenuEntityInfo.ParentID.ToString();
                this.tbxUrl.Text = MenuEntityInfo.Url.ToString();
            }
           
        }
        private void GetData(SysMenu menu)
        {
            menu.Icon = this.tbxIcon.Text.Trim();
            menu.Level = int.Parse(this.tbxLevel.Text.Trim());
            menu.Name = this.tbxMenuName.Text.Trim();
            menu.Code = this.tbxMenuEnName.Text.Trim();
            menu.Url = this.tbxUrl.Text.Trim();
            menu.OrderNo = int.Parse(this.tbxOrderNo.Text.Trim());
            menu.ParentID = new Guid(this.tbxParentID.Text.Trim());
            menu.ModifyBy = UserID;
            menu.ModifyDate = DateTime.Now;
        }
        #endregion

       
       
    }
}